using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ParallaxBackGround : MonoBehaviour
{
    private new Camera camera;
    private float moveDistance;
    private float lastMovePosition;
    private float currentMovePosition;
    private float cameraHalfWidth;
    private float cameraLeftEdge;
    private float cameraRightEdge;
    [SerializeField] private PrallaxLayer[] backGroundLayers;

    void Start()
    {
        camera = Camera.main;
        cameraHalfWidth = camera.orthographicSize * camera.aspect;
        CalculateImageWidth();
        lastMovePosition = camera.transform.position.x;
    }
    void FixedUpdate()
    {
        currentMovePosition = camera.transform.position.x;
        moveDistance = currentMovePosition - lastMovePosition;
        lastMovePosition = currentMovePosition;
        cameraLeftEdge = currentMovePosition - cameraHalfWidth;
        cameraRightEdge = currentMovePosition + cameraHalfWidth;
        foreach (var backGroundLayer in backGroundLayers)
        {
            backGroundLayer.Move(moveDistance);
            backGroundLayer.LoopBackGround(cameraLeftEdge,cameraRightEdge);
        }
    }
    private void CalculateImageWidth()
    {
        foreach (var backGroundLayer in backGroundLayers)
        {
            backGroundLayer.CalculateImageWidth();
        }
    }
}
